package top.yangyitao.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import top.yangyitao.dto.PaySlipDto;
import top.yangyitao.dto.ResultDto;
import top.yangyitao.dto.TableDto;
import top.yangyitao.dto.UserDto;
import top.yangyitao.entity.PaySlip;
import top.yangyitao.entity.User;
import top.yangyitao.service.PaySlipService;
import top.yangyitao.service.UserService;

@RestController
public class UserPaySlipController {

	@Autowired
	PaySlipService userPaySlipService;
	@Autowired
	UserService userService;
	
	@RequiresAuthentication
	@RequestMapping("/userPaySlip_html")
	public ModelAndView userPaySlip_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/userPaySlip.html");
		return mv;
	}
	
	@RequiresAuthentication
	@RequestMapping("/getUserPaySlipPage")
	public TableDto<PaySlipDto> getPaySlipPage(Integer pageNumber,Integer pageSize,String startDay,String endDay,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		Page<PaySlip> pageResult = userPaySlipService.getPaySlipPage(pageNumber,pageSize,null,userDto.getId(),startDay,endDay,search);
		List<PaySlip> userPaySlips = pageResult.getContent();
		List<PaySlipDto> userPaySlipDtos = new ArrayList<PaySlipDto>();
		userPaySlips.forEach(userPaySlip->userPaySlipDtos.add(new PaySlipDto().convert(userPaySlip)));
		return new TableDto<PaySlipDto>(userPaySlipDtos,pageResult.getTotalElements());
	}
}
